#模型数据库操作相关


import django
import sys


django.setup()
from django.contrib.auth.models import User
from serverworker.models import testmod, Device_info, Device_attribute
from serverworker import models
import os
from django.core import serializers
from django.forms.models import model_to_dict
from django.http import JsonResponse
import json
import matrix.confs.conf_network as conf
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'matrix.settings')  # 添加了环境




def fun_insert():
    #create
    fun = testmod.objects.create(name='runoob')
    print("当前行号：",sys._getframe().f_lineno,fun)

    #bulk_create
    dic =[testmod(name="m1"),testmod(name="m2"),testmod(name="m3")]
    testmod.objects.bulk_create(dic)

    #get_or_cate
    testmod.objects.get_or_create(name="m8") #当name="m8" 存在多个会报错

    #update_or_create
    testmod.objects.update_or_create(name="m8",defaults={'file':"aa"})

    #save
    s1 = testmod(name='save',file="sav1")
    s1.save()

def fun_delete():
    a = testmod(id=36)
    a = a.delete()
    print("当前行号：",sys._getframe().f_lineno,a)

    b = testmod.objects.filter(name='m1').delete()
    print("当前行号：",sys._getframe().f_lineno,b)

def fun_update():
    a = testmod.objects.filter(name='m2').update(file='update')
    print("当前行号：",sys._getframe().f_lineno,a)

    #bulk_update

def fun_select():
    #get
    a = testmod.objects.get(name='save') #当name="m8" 存在多个会报错
    print("当前行号：",sys._getframe().f_lineno,a.name,a.file,a.date)

    #earliest 返回排序后最前面一条
    b = Device_info.objects.earliest('create_time')
    print("当前行号：",sys._getframe().f_lineno,b)

    b1 = Device_info.objects.filter(scene='停车场')
    for i in b1[:3]:
        print("当前行号：",sys._getframe().f_lineno,i.company,i.device_name,i.create_user)

    c = Device_info.objects.filter(scene='停车场').latest('create_time')
    print("当前行号：",sys._getframe().f_lineno,c)

    d = Device_info.objects.filter(scene='停车场').in_bulk()
    print("当前行号：",sys._getframe().f_lineno,d)

    #values 如果没有指定字段，每个字典将包含数据库表中所有字段的键和值。
    d = Device_info.objects.filter(scene='停车场').values('company','device_name','create_user','create_time')
    for i in d[:9]:
        print("当前行号：",sys._getframe().f_lineno,i)


    e = Device_info.objects.all()
    for i in e[:3]:
        print("当前行号：",sys._getframe().f_lineno,i.company,i.create_user,i.create_time)

    # https://blog.csdn.net/qq_42022528/article/details/104262432

#格式化查询结果
def select_realze():
    device_id = 'bb5cf0a68e4572a5'
    print(device_id)

    # Proxys.objects.all() 返回的是QuerySet 可遍历转json 或者serializers
    data = models.Device_info.objects.filter(device_id=device_id).all()
    for i in data:
        print(model_to_dict(i))
    dic = serializers.serialize("json", data)
    print(json.loads(dic))

    # Proxys.objects.get(id=1) 返回的是Model 可直接转json
    data1 = models.Device_info.objects.get(device_id=device_id)
    print(model_to_dict(data1))

    data2  = models.Device_info.objects.filter(device_id=device_id).all().values()
    data2 = list(data2)
    print(data2)
    print(JsonResponse({'jsdata':data2}))


def test():
    data = models.Device_info.objects.all()
    print(data.values())
    data = models.Device_info.objects.filter(device_id=device_id).all()
    print(data[0])
    print(data.values())
    print(data.values()[0])
    print(data.values()[0]['company_id'])
    for i in data:
        print(i.company,i.device_name,i.create_user,i.create_time)
        a = [i.company,i.device_name,i.create_user,i.create_time]
        print(a)



    for i in data:
        print(i,i.company,i.scene,i.project,i.device_name,i.create_user,i.watch_user,i.create_time)

    data = models.Device_info.objects.all()
    #json_data = serializers.serialize('json', [data, ])

    from django.http import JsonResponse
    data = models.Device_info.objects.filter(device_id=device_id).first()
    print(1)
    print(data)


    #Proxys.objects.all() 返回的是QuerySet 可遍历转json 或者serializers
    data = models.Device_info.objects.filter(device_id=device_id).all()
    for i in data:
        print(model_to_dict(i))
    dic = serializers.serialize("json", data)
    print( json.loads(dic))

    #Proxys.objects.get(id=1) 返回的是Model 可直接转json
    data1 = models.Device_info.objects.get(device_id=device_id)
    print(model_to_dict(data1))

if __name__ == '__main__':
    print('views')

    device_id = 'bb5cf0a68e4572a5'
    print(device_id)

    # 返回选项
    lis_all = []
    a = models.organ_index_company.objects.all().values_list('company','id')
    company = list(a)
    print(company)

    a = models.organ_index_scene.objects.all().values_list('scene', 'id')
    scene = list(a)
    print(scene)

    a = models.organ_index_project.objects.all().values_list('project', 'id')
    project = list(a)
    print(project)

    a = models.organ_index_class.objects.all().values_list('device_class','id')
    clas = list(a)
    print(clas)

    lis_all.append(company)
    lis_all.append(scene)
    lis_all.append(project)
    lis_all.append(clas)

    print(lis_all)